/*
 * @Version: 1.0
 * @Autor:
 * @Date: 2021-02-26 16:40:22
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-11-26 16:29:25
 * @Description:
 */
import request from '@/utils/request'

// 平台列表
export function platformList() {
  return new Promise(function(resolve, reject) {
    request.get('/platforms', {
      params: {
        limit: 100000000
      }
    })
      .then(response => {
        const dataArr = response.data.map(arr => {
          arr.label = arr.name
          arr.value = arr.id
          return arr
        })
        resolve(dataArr)
      })
      .catch(() => {
        reject([])
      })
  })
}

// 客户公司列表
export function companyList(name) {
  return new Promise(function(resolve, reject) {
    request
      .get('/company', {
        params: {
          limit: 100000000,
          name: name
        }
      })
      .then(response => {
        const dataArr = response.data.map(arr => {
          arr.label = arr.name
          arr.value = arr.id
          return arr
        })
        resolve(dataArr)
      })
      .catch(() => {
        reject([])
      })
  })
}

// 所有币别列表
export function currencyAll(name) {
  return new Promise(function(resolve, reject) {
    request
      .get('currencyAll', {
        params: {
          limit: 100000000,
          name: name
        }
      })
      .then(res => {
        const dataArr = res.data.map(arr => {
          arr.label = arr.source_name
          arr.value = arr.id + ',' + arr.source_name
          return arr
        }).filter((item) => {
          return item.is_status === 1
        })
        resolve(dataArr)
      })
      .catch(() => {
        reject([])
      })
  })
}

// 虚拟仓库
export function warehouseList(name) {
  return new Promise(function(resolve, reject) {
    request
      .get('warehouse', {
        params: {
          limit: 100000000,
          name: name
        }
      })
      .then(res => {
        const dataArr = res.data.map(arr => {
          arr.label = arr.name
          arr.value = arr.id
          return arr
        }).filter((item) => {
          return item.type === 2 && item.is_active === 1
        })
        resolve(dataArr)
      })
      .catch(() => {
        reject([])
      })
  })
}
// 实体仓库
export function warehouseListReal() {
  return new Promise(function(resolve, reject) {
    request
      .get('warehouse', {
        params: {
          limit: 100000000,
          type: 1
        }
      })
      .then(res => {
        const dataArr = res.data
          .map(arr => {
            arr.label = arr.name
            arr.value = arr.id
            return arr
          })
        resolve(dataArr)
      })
      .catch(() => {
        reject([])
      })
  })
}
// 查询组合 seeBindWarehouse
export function seeBindWarehouse(id, name) {
  return new Promise(function(resolve, reject) {
    request
      .get(`${name}/seeBindWarehouse/${id}`)
      .then(res => {
        const dataArr = res.data
        resolve(dataArr)
      })
      .catch(() => {
        reject([])
      })
  })
}

// 查询绑定用户 seeBindUser
export function seeBindUser(id) {
  return new Promise(function(resolve, reject) {
    request
      .get(`shops/seeBindUser/${id}`)
      .then(res => {
        const dataArr = res.data
        resolve(dataArr)
      })
      .catch(() => {
        reject([])
      })
  })
}

// 用户列表  /user/list
export function userList(username = '') {
  return new Promise(function(resolve, reject) {
    request
      .get(`/user/list`, {
        params: {
          limit: 1000,
          username
        }
      })
      .then(res => {
        const dataArr = res.data.filter((item) => {
          return (
            item.identify !== 'warehouseman' &&
            item.identify !== 'buyerStaff'
          )
        })
        resolve(dataArr)
      })
      .catch(() => {
        reject([])
      })
  })
}

// 州
export function statesList() {
  return new Promise(function(resolve, reject) {
    request
      .get(`/states`, {
        params: {
          limit: 100000000,
          page: 1
        }
      })
      .then(res => {
        const dataArr = res.data
        resolve(dataArr)
      })
      .catch(() => {
        reject([])
      })
  })
}
// 城市
export function cityList(id) {
  return new Promise(function(resolve, reject) {
    request
      .get(`/city`, {
        params: {
          states_id: id
        }
      })
      .then(res => {
        const dataArr = res.data
        resolve(dataArr)
      })
      .catch(() => {
        reject([])
      })
  })
}
// 获取城市编码
export function cityPostalCode(id) {
  return new Promise(function(resolve, reject) {
    request
      .get(`/cityPostalCode/${id}`)
      .then(res => {
        const dataArr = res.data.map((e) => {
          e['value'] = e.zip
          return e
        })
        resolve(dataArr)
      })
      .catch(() => {
        reject([])
      })
  })
}

// 查询店铺 (根据平台id)
export function shopList(id) {
  return new Promise(function(resolve, reject) {
    request
      .get(`/shops/getShopPlatform/${id}`)
      .then(res => {
        const dataArr = res.data
        resolve(dataArr)
      })
      .catch(() => {
        reject([])
      })
  })
}
// 订单选择商品 products/list
export function productsList(shopId) {
  return new Promise(function(resolve, reject) {
    request
      .get(`products/list?shop_id=${shopId}`)
      .then(res => {
        const dataArr = res.data.filter((item) => {
          return item.shop_id === shopId
        })
        resolve(dataArr)
      })
      .catch(() => {
        reject([])
      })
  })
}
// 订单选择组合商品 orderRecord/combinationProductList/78
export function combinationProductList(shopId) {
  return new Promise(function(resolve, reject) {
    request
      .get(`orderRecord/combinationProductList/${shopId}`)
      .then(res => {
        const dataArr = res.data
        resolve(dataArr)
      })
      .catch(() => {
        reject([])
      })
  })
}

// 街道
export function streetList(params) {
  return new Promise(function(resolve, reject) {
    resolve([{
      id: 123,
      name: '成华大道'
    },
    {
      id: 124,
      name: '科技一路'
    },
    {
      id: 125,
      name: '科技二路'
    },
    {
      id: 126,
      name: '科技三路'
    }
    ])
  })
}

// 商品关联的配件列表
export function partListProduct(id) {
  return new Promise(function(resolve, reject) {
    request
      .get(`parts/partListProduct/${id}`)
      .then(res => {
        const dataArr = res.data
        resolve(dataArr)
      })
      .catch(() => {
        reject([])
      })
  })
}

// 获取店铺绑定的实体仓库
export function warehouseShop(shop_id) {
  return new Promise(function(resolve, reject) {
    request
      .get(`/afterSaleOrders/warehouseShop`, {
        params: {
          shop_id: shop_id
        }
      })
      .then(res => {
        const dataArr = res.data.map((arr) => {
          arr.label = arr.entity_name
          arr.value = arr.warehouse_id
          return arr
        })
        resolve(dataArr)
      })
      .catch(() => {
        reject([])
      })
  })
}

// 获取售后订单可入库仓库
export function warehousesSubclass(shop_id, storage_id) {
  return new Promise(function(resolve, reject) {
    request
      .get(`/afterSaleOrders/warehousesSubclass`, {
        params: {
          shop_id: shop_id,
          storage_id: storage_id
        }
      })
      .then(res => {
        const dataArr = res.data
        resolve(dataArr)
      })
      .catch(() => {
        reject([])
      })
  })
}

// 获取物流信息
export function deliveryUpsTracking(code) {
  return new Promise(function(resolve, reject) {
    request
      .post(`/deliveryUpsTracking`, {
        shipping_code: code
      })
      .then(res => {
        if (res.code === 10000) {
          const dataArr = res.data
          resolve(dataArr)
        } else {
          resolve(false)
        }
      })
      .catch(() => {
        reject([])
      })
  })
}
// 获取Usps物流信息;
export function deliveryUspsTracking(code) {
  return new Promise(function(resolve, reject) {
    request
      .post(`/deliveryUspsTracking`, {
        shipping_code: code
      })
      .then(res => {
        if (res.code === 10000) {
          const dataArr = res.data
          resolve(dataArr)
        } else {
          resolve(false)
        }
      })
      .catch(() => {
        reject([])
      })
  })
}

// 获取售后订单 deliveryUpsTracking
export function deliveryUpsTrackingAfter(code) {
  return new Promise(function(resolve, reject) {
    request
      .get(`/afterSaleOrders/getUpsInfo`, {
        params: {
          shipping_code: code
        }
      })
      .then(res => {
        console.log('--------------')
        if (res.code === 10000) {
          const dataArr = res.data
          resolve(dataArr)
        } else {
          resolve(false)
        }
      })
      .catch(() => {
        reject([])
      })
  })
}

// 获取物流信息
export function lforwarderData() {
  return new Promise(function(resolve, reject) {
    request
      .get(`/lforwarder`, {
        params: {
          limit: 100,
          page: 1,
          type: 1,
          status: 1
        }
      })
      .then(res => {
        const dataArr = res.data
        resolve(dataArr)
      })
      .catch(() => {
        reject([])
      })
  })
}

/**
 * 获取手工发货仓库 publicData
 * @param id 发货单id
 */
export function warehouseStockList(id) {
  console.log('id', id)
  return new Promise(function(resolve, reject) {
    request
      .get(`/deliveryOrders/warehouseStockList/${id}`)
      .then(res => {
        const dataArr = res.data.filter((item) => {
          //   return item.product_number > 0
          return item
        })
        resolve(dataArr)
      })
      .catch(() => {
        reject([])
      })
  })
}
/**
 * 获取手工发货 第三方仓库 /deliveryOrders/warehouseStockListOther
 */
export function warehouseStockListOther(id) {
  return new Promise(function(resolve, reject) {
    request
      .get(`/deliveryOrders/warehouseStockListOther/${id}`)
      .then(res => {
        const dataArr = res.data.filter(item => {
          //   return item.product_number > 0
          return item
        })
        resolve(dataArr)
      })
      .catch(() => {
        reject([])
      })
  })
}
export function getVirtualWarehouseList() {
  return new Promise(function(resolve, reject) {
    const params = {
      page: 1,
      limit: 100000000,
      type: 2, // 1-实体仓 2-虚拟仓 3-残品仓 4-FBA仓
      is_active: 1
    }
    request
      .get('/warehouse', { params })
      .then(res => {
        const virtualWarehouseList = res.data
        resolve(virtualWarehouseList)
      })
      .catch(() => {
        reject([])
      })
  })
}

export function getEntityWarehouseList() {
  return new Promise(function(resolve, reject) {
    const params = {
      page: 1,
      limit: 100000000,
      type: 1, // 1-实体仓 2-虚拟仓 3-残品仓 4-FBA仓
      is_active: 1
    }
    request
      .get('/warehouse', { params })
      .then(res => {
        resolve(res.data)
      })
      .catch(() => {
        reject([])
      })
  })
}
// 获取第三方仓库 warehouse/all
export function getWarehouseThrid() {
  return new Promise(function(resolve, reject) {
    const params = {
      is_third_part: 1
    }
    request
      .get('warehouse/all', { params })
      .then(res => {
        const list = []
        res.data.map((arr) => {
          if (arr.children && arr.children.length > 0) {
            arr.children.map(item => {
              list.push(item)
              //   return item
            })
          }
          //   return arr
        })
        resolve(list)
      })
      .catch(() => {
        reject([])
      })
  })
}
// 获取客户列表
export function getCompanyList() {
  return new Promise(function(resolve, reject) {
    request
      .get('orderRecord/getCompanyList')
      .then(res => {
        resolve(res.data)
      })
      .catch(() => {
        reject([])
      })
  })
}
/**
 * 获取客户商品
 * @param {*} id  客户id
 * @returns
 */
export function orderCustomerProduct(id) {
  return new Promise(function(resolve, reject) {
    request
      .get(`orderRecords/orderCustomerProduct/${id}`)
      .then(res => {
        resolve(res.data)
      })
      .catch(() => {
        reject([])
      })
  })
}
/**
 * 获取借卖店铺
 */
export function getBorrowSellShopList() {
  return new Promise(function(resolve, reject) {
    request
      .get(`/orderRecord/borrowSellShopList`)
      .then(res => {
        resolve(res.data)
      })
      .catch(() => {
        reject([])
      })
  })
}
/**
 * 获取借卖客户
 */
export function getBorrowSellCompanyList() {
  return new Promise(function(resolve, reject) {
    request
      .get(`/orderRecord/borrowSellCompanyList`)
      .then(res => {
        resolve(res.data)
      })
      .catch(() => {
        reject([])
      })
  })
}
/**
 * 借卖订单商品
 */
export function getBorrowSellGoodsList() {
  return new Promise(function(resolve, reject) {
    request
      .get(`/orderRecord/borrowSellGoodsList`)
      .then(res => {
        resolve(res.data)
      })
      .catch(() => {
        reject([])
      })
  })
}
/**
 * 批量手工发货仓库
 */
export function getWarehouse() {
  return new Promise(function(resolve, reject) {
    request
      .get(`/deliveryOrders/getWarehouse`)
      .then(res => {
        resolve(res.data)
      })
      .catch(() => {
        reject([])
      })
  })
}
/**
 * 采购员列表
 * shops/buyers
 */
export function getBuyersList() {
  return new Promise(function(resolve, reject) {
    request
      .get(`/shops/buyers`)
      .then(res => {
        resolve(res.data)
      })
      .catch(() => {
        reject([])
      })
  })
}

/**
 * 获取出运单列表
 */
export function transhipmentAll(params) {
  return new Promise(function(resolve, reject) {
    request
      .get(`/transhipment/order/all`)
      .then(res => {
        resolve(res.data)
      })
      .catch(() => {
        reject([])
      })
  })
}

/**
 * 所有的订单列表
 */
export function getAllOrder() {
  return new Promise(function(resolve, reject) {
    request
      .get(`orderRecord/getAllOrder`)
      .then(res => {
        resolve(res.data)
      })
      .catch(() => {
        reject([])
      })
  })
}

